<template>
  <div class="auth-container">
    <!-- 动态背景 -->
    <div class="background-animation"></div>

    <!-- 注册框 -->
    <div class="auth-box">
      <div class="auth-header">
        <span class="welcome-text">欢迎注册</span>
        <span class="sub-text">创建您的账号</span>
      </div>
      <el-form ref="registerFormRef" :model="registerForm" size="normal" :rules="registerRules">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="registerForm.username" placeholder="用户名" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input :prefix-icon="Message" v-model="registerForm.email" placeholder="邮箱" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="registerForm.password" show-password placeholder="密码" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input :prefix-icon="Lock" v-model="registerForm.confirmPassword" show-password placeholder="确认密码" class="custom-input"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="auth-button" type="primary" @click="register">注 册</el-button>
        </el-form-item>
      </el-form>

      <!-- 分界线 -->
      <div class="divider">
        <span class="divider-text">或</span>
      </div>

      <!-- 社交注册 -->
      <div class="social-auth">
        <el-button class="social-button wechat" @click="registerWithWechat">
          Wechat注册
        </el-button>
        <el-button class="social-button google" @click="registerWithGoogle">
          Google注册
        </el-button>
        <el-button class="social-button github" @click="registerWithGithub">
          GitHub注册
        </el-button>
      </div>

      <!-- 登录链接 -->
      <div class="switch-link">
        已有账号？<el-link type="primary" @click="switchToLogin">立即登录</el-link>
      </div>
    </div>
  </div>
</template>

<script>
import { User, Lock, Message } from '@element-plus/icons-vue';
import request from "../utils/request";

export default {
  name: "Register",
  components: {
    User,
    Lock,
    Message,
  },
  data() {
    return {
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    register() {
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.$message.error({
          message: '两次密码输入不一致',
          type: 'error',
        });
        return;
      }

      this.$refs.registerFormRef.validate(valid => {
        if (valid) {
          request.post("/user/register", this.registerForm).then(
              res => {
                if (res.code === 200) {
                  this.$message.success({
                    message: '注册成功',
                    type: 'success',
                  });
                  this.$router.push("/login");
                } else {
                  this.$message.error({
                    message: res.msg,
                    type: 'error',
                  });
                }
              }
          );
        }
      });
    },
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    switchToLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.auth-container {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #6a00ff, #ac00d3);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

/* 动态背景 */
.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 10%, transparent 10.01%);
  background-size: 20px 20px;
  animation: moveBackground 3s linear infinite;
}

@keyframes moveBackground {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(20px);
  }
}

/* 注册框 */
.auth-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  z-index: 1;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.auth-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
}

/* 标题 */
.auth-header {
  text-align: center;
  margin-bottom: 30px;
}

.welcome-text {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  display: block;
}

.sub-text {
  font-size: 14px;
  color: #666;
  margin-top: 8px;
  display: block;
}

/* 输入框 */
.custom-input {
  width: 100%;
}

.custom-input >>> .el-input__inner {
  border-radius: 8px;
  border: 1px solid #ddd;
  padding: 12px 15px;
  font-size: 14px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.custom-input >>> .el-input__inner:focus {
  border-color: #6a00ff;
  box-shadow: 0 0 8px rgba(106, 0, 255, 0.2);
}

/* 注册按钮 */
.auth-button {
  width: 100%;
  margin-top: 20px;
  background: linear-gradient(135deg, #6a00ff, #ac00d3);
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: bold;
  color: white;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.auth-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(106, 0, 255, 0.3);
}

/* 分界线 */
.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #ddd;
}

.divider-text {
  padding: 0 10px;
  color: #666;
  font-size: 14px;
}

/* 社交注册 */
.social-auth {
  display: flex;
  flex-direction: row;
  gap: 0px; /* 调整按钮之间的间距 */
}

.social-auth .social-button {
  width: 70%; /* 确保按钮宽度一致 */
  display: flex;
  align-items: center;
  justify-content: center; /* 文本居中 */
  font-size: 11px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  padding: 12px 20px; /* 统一按钮内边距 */
}

.social-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.social-button.wechat {
  background-color: #07c160;
  color: white;
}

.social-button.google {
  background-color: #db4437;
  color: white;
}

.social-button.github {
  background-color: #333;
  color: white;
}

/* 切换链接 */
.switch-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.switch-link .el-link {
  font-size: 14px;
}
</style>